<?php
!$opration && $opration = 'list';
$message = $messages = array();
cpheader();
if ($opration == 'toggle_audited'){
	$message['tid'] = max(array(0,intval(getpc('tid'))));
	$message = $db->get_one("SELECT audited FROM sdw_subject WHERE tid=$message[tid]");
	$message['audited'] = $message['audited']==1 ? 0 : 1;
	$db->query("UPDATE sdw_subject SET audited='$message[audited]' WHERE tid=$message[tid]");
	dexit($message['tid']);
}
if ($opration == 'drop'){
	$message['tid'] = isset($_GET['id']) ? trim($_GET['id']) : 0;
	$db->query("DELETE FROM sdw_posts WHERE tid IN ($message[tid])");
	$db->query("DELETE FROM sdw_posts WHERE tid IN ($message[tid])");
	$opration = 'list';
}
if ($opration == 'list'){
	$filter = array();
	$filter['pagesize'] = 20;
	$filter['fid'] = max(array(0,intval(getpc('fid'))));
	$filter['key'] = getpc('q');
	$wheresql = $filter['fid']>0 ? "AND fid=$filter[fid]" : '';
	$data = $db->get_one("SELECT COUNT(*) FROM sdw_subject WHERE subject LIKE '%$filter[key]%' $wheresql");
	$filter['pagecount'] = $data['COUNT(*)']<$filter['pagesize'] ? 1 : ceil($data['COUNT(*)']/$filter['pagesize']);
	$filter['page'] = min(array($page,$filter['pagecount']));
	$filter['limit'] = ($filter['page']=1)*$filter['pagesize'];
	$query = $db->query("SELECT * FROM sdw_subject WHERE subject LIKE '%$filter[key]%' $wheresql ORDER BY tid DESC LIMIT $filter[limit],$filter[pagesize]");
	while ($result = $db->fetch_array($query)){
		$messages[] = $result;
	}
	$smarty->assign('filter',$filter);
	$smarty->assign('page',$filter['page']);
	$smarty->assign('records',$data['COUNT(*)']);
	$smarty->assign('messages',$messages);
	$smarty->assign('pagelink',page_ajax($filter['page'],$filter['pagecount'],"fid=$filter[fid]&q=$filter[key]"));
}
$smarty->assign('opration',$opration);
$smarty->display('admin_subject.htm');
cpfooter();
?>